package com.wrx.server.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wrx.server.mapper.BarMapper;
import com.wrx.server.mapper.CollectMapper;
import com.wrx.server.mapper.ThemePostMapper;
import com.wrx.server.pojo.*;
import com.wrx.server.service.ICollectService;
import com.wrx.server.service.IThemePostService;
import com.wrx.server.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wrx
 * @since 2021-04-20
 */
@Service
public class ThemePostServiceImpl extends ServiceImpl<ThemePostMapper, ThemePost> implements IThemePostService {

    @Autowired
    IUserService userService;

    @Autowired
    BarMapper barMapper;

    @Autowired
    ThemePostMapper themePostMapper;

    @Autowired
    ICollectService collectService;

    @Autowired
    CollectMapper collectMapper;
    String sortType1 = "theme_post_id";
    String sortType2 = "last_reply_time";

    @Override
    public RespBean setThemePost(Integer bar_id, String theme_post_title, String theme_post_info,HttpServletRequest request) {

        if (barMapper.selBarIdByBarId(bar_id)!=1) {
            return RespBean.error("该贴吧不存在，请重试");
        }
        if ( StringUtils.isBlank(theme_post_title) ) {
            return RespBean.error("帖子标题为空，请重试");
        }
        if (StringUtils.isBlank(theme_post_info )) {
            return RespBean.error("帖子内容为空，请重试");
        }

        Integer user_id = userService.getUserInfo(request.getUserPrincipal()).getUserId();
        if (null!=user_id) {
            themePostMapper.insertThemePost(bar_id,user_id,theme_post_title,theme_post_info);
            barMapper.addBarPostNumber(bar_id);
            return RespBean.success("发表帖子成功");
        }


        return RespBean.error("发表帖子出现异常错误，请重试");
    }

    @Override
    public RespBean putPageTheme(int bar_id, int pageNum, int pageSize,int state) {

        PageHelper.startPage(pageNum, pageSize);
        List<ThemePostModel> lists;

        //state为1则根据发帖时间排序(用帖子id效率更高)，为2则根据最后回复时间排序，默认2
        if(state == 1) {
            lists= themePostMapper.selPutTimeAllThemePost(bar_id,sortType1);
        }else{
            lists = themePostMapper.selLastTimeAllThemePost(bar_id,sortType2);
        }
        PageInfo<ThemePostModel> pageInfo = new PageInfo<ThemePostModel>(lists);
        return RespBean.success(pageInfo);
    }

    @Override
    public RespBean selOneThemePost(int theme_post_id) {
        if (theme_post_id<=0){
            return RespBean.error("该主题帖不存在");
        }
        List<ThemePost> list = themePostMapper.selOneThemePost(theme_post_id);
        return RespBean.success(list);
    }

    @Override
    public RespBean selOneThemePostAndNetNameByThemePostId(int theme_post_id) {
        if (theme_post_id<=0){
            return RespBean.error("该主题帖不存在");
        }
        List<ThemePostModel> list = themePostMapper.selOneThemePostAndNetNameByThemePostId(theme_post_id);
        if (null==list){
            return RespBean.error("该帖子不存在，请重试");
        }else {
            return RespBean.success(list);
        }
    }

    @Override
    public RespBean selOneThemePostNetNameCollectorStateByThemePostId(int theme_post_id,HttpServletRequest request) {
        System.out.println(theme_post_id+"      aaa");
        if (theme_post_id<=0){
            return RespBean.error("该主题帖不存在");
        }
        List<ThemePostModel> list = themePostMapper.selOneThemePostAndNetNameByThemePostId(theme_post_id);
        System.out.println("bbbbb");
        if (null==list){
            return RespBean.error("该帖子不存在，请重试");
        }

        Integer userId = userService.getUserInfo(request.getUserPrincipal()).getUserId();
        System.out.println(userId);
        System.out.println("ccc");
        if (userId == null){
            return RespBean.error("收藏失败，请先登录！");
        }

        Collect collect = new Collect();
        collect.setUser_id(userId);
        collect.setTheme_post_id(theme_post_id);

        Integer collectId = collectMapper.selCollectId(collect);
        CollectVerifyModel collectVerifyModel = new CollectVerifyModel();
        collectVerifyModel.setObject(list);
        if (collectId==null){
            collectVerifyModel.setCollect_state(false);
            return RespBean.success(collectVerifyModel);
        }else {
            collectVerifyModel.setCollect_state(true);
            return RespBean.success(collectVerifyModel);
        }
    }
}
